package com.gxc.authcore.system.util;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.gxc.webcore.util.RequestUtil;

/**
 * 序列化工具
 * 
 * @author gxc
 * 
 */
public class SerializeUtil {

	/**
	 * 序列化参数
	 * 
	 * @param params
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public static String serializeParam(Map params) {
		StringBuffer sb = new StringBuffer();
		int i = 0;
		for (Object key : params.keySet()) {
			if (i == 0) {
				sb.append("?");
			} else if (i > 0) {
				sb.append("&");
			}
			sb.append((String) key);
			sb.append("=");
			sb.append((String) params.get(key));
			i++;
		}
		return sb.toString();
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static String serializeRedirectParam(HttpServletRequest request) {

		Map params = RequestUtil.getInMap(request);

		// 获取认证后的跳回上次的请求地址
		String clientRequestUrl = null;
		String url = request.getPathInfo() == null ? request.getServletPath()
				: request.getPathInfo();
		url = url.trim();
		while (url.startsWith("/")) {
			url = url.substring(1);
		}
		clientRequestUrl = ParamUtil.get("url_local") + "/" + url;
		params.put("clientRequestUrl", clientRequestUrl);

		// 客户端系统url
		String clientSystemUrl = ParamUtil.get("url_local");
		params.put("clientSystemUrl", clientSystemUrl);

		return SerializeUtil.serializeParam(params);
	}

}
